<?php

/*
	Class: Watch_IndexController

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Watch_IndexController extends Cts_Controller_Action_Abstract {

	/* Group: Instance Methods */

	function init(){
		parent::init();
	}

	/* Group: Actions */

	/*
		Function: index
			Displays a list of all feed items for a particular user.
			If paging is enabled, the list will be paged based on the page size set in the module config.

		View Variables:
			feeditems - An array of feed items.
	*/
	function indexAction() {
		$username = $this->_identity->username;
		$feeditems_table = new WatchFeedItems();
		if (Cts_Registry::get('enable_paging') == 1) {
			$request = new Cts_Request($this->getRequest());
			if ($request->has('page')) {
				$page = $request->page;
			} else {
				$page = 0;
			}
	        $per_page = Cts_Registry::get('feed_items_per_page');
			$total = $feeditems_table->getItemsCountByUsername($username);
			$url = Cts_Registry::get('feed_index_url');
			$params = array(); // don't need any params right now
			$this->makePager($page, $per_page, $total, $url, $params);
			$this->view->feeditems = $feeditems_table->getItemsPage($username, $per_page, $page);
			$this->view->enable_paging = true;
		} else {
			$this->view->feeditems = $feeditems_table->getItems($username);
			$this->view->enable_paging = false;
		}
	}

	/*
		Function: rss
			A list of all feed items for a particular user in RSS 2.0 XML format.

		View Variables:
			posts - The array of feed items. We call them "feed items", RSS calls them "posts".
			username - The username of the user to whom the feed belongs.
			feed_title - The title of the feed as pulled from the registry.
			feed_description - The description of the feed as pulled from the module config.
	*/
	function rssAction() {
		$request = new Cts_Request($this->getRequest());
		$feeditems_table = new WatchFeedItems();
		$tmp_posts = $feeditems_table->getItems($username);
		$this->view->posts = $tmp_posts;
		$this->view->username = $request->username;
		$this->view->feed_title = Cts_Registry::get('feed_rss_title');
		$this->view->feed_description = Cts_Registry::get('feed_rss_description');
		header('Content-Type: application/rss+xml');
	}

}
